package cn.jiedh.app.meiweidk.welcome;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jiedh.app.meiweidk.MainActivity;
import cn.jiedh.app.meiweidk.R;
import cn.jiedh.app.meiweidk.Utils.tool.ActivityUtil;
import cn.jiedh.app.meiweidk.Utils.tool.SPUtils;
import cn.jiedh.app.meiweidk.Utils.tool.StatusBarUtil;
import cn.jiedh.app.meiweidk.Utils.tool.T;
import cn.jiedh.app.meiweidk.base.BaseActivity;
import cn.jiedh.app.meiweidk.login.LoginActivity;

/**
 * @name：
 * @author： 周毅
 * @phone： 15826080712
 * @createTime： 2017/5/24
 * @modifyTime： 2017/5/24
 * @explain：
 */
public class GuangGaoActivity extends BaseActivity {

    @BindView(R.id.convenientBanner)
    ConvenientBanner convenientBanner;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.ll_going)
    LinearLayout llGoing;
    private CountDownTimer time;

    @Override
    protected void initView(View view) {
        StatusBarUtil.transparencyBar(this);
    }

    /**
     * 初始化banner
     */
    private void initadver() {
        AdverBean adverBean = SPUtils.getadver();
        List<AdverBean> adverBeenlist = new ArrayList<>();
        adverBeenlist.add(adverBean);
        convenientBanner.setCanLoop(false);
        convenientBanner.setPages(new CBViewHolderCreator<AdverHolderView>() {
            @Override
            public AdverHolderView createHolder() {
                return new AdverHolderView();
            }
        }, adverBeenlist)
//                .setPageIndicator(new int[]{R.drawable.dot2, R.drawable.dot})
            .setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    if (!adverBeenlist.get(position).getLink().isEmpty()) {
                        time.cancel();
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("adver", adverBeenlist.get(position));
                        bundle.putInt("page", 1);
                        bundle.putBoolean("onclick", true);
                        if (SPUtils.getIsLogin()) {
                            ActivityUtil.next(activity, MainActivity.class, bundle);
                        } else {
                            T.showShort("未登录，请先登录");
                            bundle.putBoolean("isGoHome", true);
                            ActivityUtil.next(activity, LoginActivity.class, bundle);
                        }

                        finish();

                    }
                }
            })
            .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL);
    }

    @Override
    protected void initData() {

        initadver();
        time = new CountDownTimer(3000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                tvNum.setText("(" + millisUntilFinished / 1000 + ")");
            }

            @Override
            public void onFinish() {
                time.cancel();
                if (SPUtils.getIsLogin()) {
                    ActivityUtil.next(activity, MainActivity.class);
                } else {
//                    T.showShort("未登录，请先登录");
                    Bundle bundle = new Bundle();
                    bundle.putBoolean("isGoHome", true);
                    ActivityUtil.next(activity, LoginActivity.class, bundle);
                }
                finish();
            }
        };
        time.start();
        llGoing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                time.cancel();
                if (SPUtils.getIsLogin()) {
                    ActivityUtil.next(activity, MainActivity.class);
                } else {
//                    T.showShort("未登录，请先登录");
                    Bundle bundle = new Bundle();
                    bundle.putBoolean("isGoHome", true);
                    ActivityUtil.next(activity, LoginActivity.class, bundle);
                }
                finish();
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation

        ButterKnife.bind(this);
    }

    @Override
    protected int getLayoutView() {
        return R.layout.guanggao;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        try {
            System.exit(0);
        } catch (RuntimeException e) {

        }
    }

}
